package com.sc.study.sleeproom.service.impl;

import java.util.*;
import java.util.logging.SimpleFormatter;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.alibaba.fastjson.JSON;
import com.sc.common.core.domain.entity.SysUser;
import com.sc.common.utils.DateUtils;
import com.sc.study.ar.service.IScViolationRegisterService;
import com.sc.study.sleeproom.domain.Person;
import com.sc.study.sleeproom.domain.ScSleepRoomPersonChangeLog;
import com.sc.study.sleeproom.mapper.ScSleepRoomMapper;
import com.sc.study.sleeproom.mapper.ScSleepRoomPersonTypeMapper;
import com.sc.study.sleeproom.service.IScSleepRoomPersonChangeLogService;
import com.sc.study.student.domain.ScStudent;
import com.sc.study.student.mapper.ScStudentMapper;
import com.sc.system.service.ISysConfigService;
import com.sc.system.service.ISysUserService;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sc.study.sleeproom.mapper.ScSleepRoomPersonMapper;
import com.sc.study.sleeproom.domain.ScSleepRoomPerson;
import com.sc.study.sleeproom.service.IScSleepRoomPersonService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 寝室人员Service业务层处理
 * 
 * @author sc
 * @date 2022-03-14
 */
@Service
public class ScSleepRoomPersonServiceImpl implements IScSleepRoomPersonService 
{
    @Autowired
    private ScSleepRoomPersonMapper scSleepRoomPersonMapper;

    @Autowired
    private ScStudentMapper scStudentMapper;

    @Autowired
    private ScSleepRoomMapper scSleepRoomMapper;

    @Autowired
    private ScSleepRoomPersonTypeMapper scSleepRoomPersonTypeMapper;

    @Autowired
    private IScSleepRoomPersonChangeLogService changeLogService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private IScViolationRegisterService violationRegisterService;

    /**
     * 查询寝室人员
     * 
     * @param id 寝室人员主键
     * @return 寝室人员
     */
    @Override
    public ScSleepRoomPerson selectScSleepRoomPersonById(Long id)
    {
        return scSleepRoomPersonMapper.selectScSleepRoomPersonById(id);
    }

    /**
     * 查询寝室人员列表
     * 
     * @param scSleepRoomPerson 寝室人员
     * @return 寝室人员
     */
    @Override
    public List<ScSleepRoomPerson> selectScSleepRoomPersonList(ScSleepRoomPerson scSleepRoomPerson)
    {
        List<ScSleepRoomPerson> scSleepRoomPeopleList = scSleepRoomPersonMapper.selectScSleepRoomPersonList(scSleepRoomPerson);

        List<Long> sleep_room_person_type_stu = JSON.parseArray(sysConfigService.selectConfigByKey("sleep_room_person_type_stu"), Long.class);
        List<Long> sleep_room_person_type_teacher = JSON.parseArray(sysConfigService.selectConfigByKey("sleep_room_person_type_teacher"), Long.class);


        List<ScSleepRoomPerson> removeRoomPerson = new ArrayList<>();
        //设置寝室人员类型 学生 寝室
        scSleepRoomPeopleList.forEach(item -> {
            //设置学生信息
            if(sleep_room_person_type_stu.contains(item.getSleeproompersontypeid())){
                ScStudent scStudent = scStudentMapper.selectScStudentById(item.getPersonid());
                item.setPerson(new Person(scStudent.getId(), scStudent.getStuname()));
            }else if(sleep_room_person_type_teacher.contains(item.getSleeproompersontypeid())){      //设置老师信息
                SysUser sysUser = userService.selectUserById(item.getPersonid());
                item.setPerson(new Person(sysUser.getUserId(), sysUser.getNickName()));
            }else{
                throw new NullPointerException("您没有设置寝室人员类型的字典参数哦");
            }

            //设置寝室信息
            item.setScSleepRoom(scSleepRoomMapper.selectScSleepRoomById(item.getSleeproomid()));
            //寝室人员类型
            item.setScSleepRoomPersonType(scSleepRoomPersonTypeMapper.selectScSleepRoomPersonTypeById(item.getSleeproompersontypeid()));

            //模糊查询人员名称
            if(scSleepRoomPerson.getLikePersonName() != null && scSleepRoomPerson.getLikePersonName() != ""){
                boolean contains = item.getPerson().getName().contains(scSleepRoomPerson.getLikePersonName());
                if(!contains) removeRoomPerson.add(item);
            }


        });
        removeRoomPerson.forEach(item -> scSleepRoomPeopleList.remove(item));


        return scSleepRoomPeopleList;
    }


    /**
     * 查询寝室人员列表 不包含老师
     *
     * @param scSleepRoomPerson 寝室人员
     * @return 寝室人员
     */
    @Override
    public List<ScSleepRoomPerson> selectScSleepRoomPersonNotTeacherList(ScSleepRoomPerson scSleepRoomPerson)
    {
        List<ScSleepRoomPerson> scSleepRoomPeopleList = scSleepRoomPersonMapper.selectScSleepRoomPersonList(scSleepRoomPerson);

        List<Long> sleep_room_person_type_stu = JSON.parseArray(sysConfigService.selectConfigByKey("sleep_room_person_type_stu"), Long.class);

        List<ScSleepRoomPerson> removeRoomPerson = new ArrayList<>();
        //设置寝室人员类型 学生 寝室
        scSleepRoomPeopleList.forEach(item -> {
            //设置学生信息
            if(sleep_room_person_type_stu.contains(item.getSleeproompersontypeid())){
                ScStudent scStudent = scStudentMapper.selectScStudentById(item.getPersonid());
                item.setPerson(new Person(scStudent.getId(), scStudent.getStuname()));

                //设置寝室信息
                item.setScSleepRoom(scSleepRoomMapper.selectScSleepRoomById(item.getSleeproomid()));
                //寝室人员类型
                item.setScSleepRoomPersonType(scSleepRoomPersonTypeMapper.selectScSleepRoomPersonTypeById(item.getSleeproompersontypeid()));

                //今日是否登记缺勤
                item.setIfCheckRoom(violationRegisterService.isCheckRoom(item.getPerson().getId()));
            }else{
                removeRoomPerson.add(item);
            }
        });
        removeRoomPerson.forEach(item -> scSleepRoomPeopleList.remove(item));


        return scSleepRoomPeopleList;
    }


    /**
     * 新增寝室人员
     * 
     * @param scSleepRoomPerson 寝室人员
     * @return 结果
     */
    @Override
    @Transactional
    public int insertScSleepRoomPerson(ScSleepRoomPerson scSleepRoomPerson)
    {
        return scSleepRoomPersonMapper.insertScSleepRoomPerson(scSleepRoomPerson);
    }

    /**
     * 修改寝室人员
     * 
     * @param scSleepRoomPerson 寝室人员
     * @return 结果
     */
    @Override
    @Transactional
    public int updateScSleepRoomPerson(ScSleepRoomPerson scSleepRoomPerson)
    {
        ScSleepRoomPerson scSleepRoomPerson1 = scSleepRoomPersonMapper.selectScSleepRoomPersonById(scSleepRoomPerson.getId());
        int i = scSleepRoomPersonMapper.updateScSleepRoomPerson(scSleepRoomPerson);

        //增加日志
        ScSleepRoomPersonChangeLog scSleepRoomPersonChangeLog = new ScSleepRoomPersonChangeLog();
        scSleepRoomPersonChangeLog.setParams(scSleepRoomPerson.getId(), scSleepRoomPerson1.getSleeproompersontypeid(), scSleepRoomPerson.getOldSleepRoomId(), scSleepRoomPerson.getSleeproomid(), "修改");
        changeLogService.insertLog(scSleepRoomPersonChangeLog);

        return i;
    }

    /**
     * 批量删除寝室人员
     * 
     * @param ids 需要删除的寝室人员主键
     * @return 结果
     */
    @Override
    public int deleteScSleepRoomPersonByIds(Long[] ids)
    {
        for (Long id : ids) {
            ScSleepRoomPerson scSleepRoomPerson = scSleepRoomPersonMapper.selectScSleepRoomPersonById(id);

            scSleepRoomPersonMapper.deleteScSleepRoomPersonById(id);

            //增加日志
            ScSleepRoomPersonChangeLog scSleepRoomPersonChangeLog = new ScSleepRoomPersonChangeLog();
            scSleepRoomPersonChangeLog.setParams(scSleepRoomPerson.getId(), scSleepRoomPerson.getSleeproompersontypeid(), scSleepRoomPerson.getSleeproomid(), null, "删除");
            changeLogService.insertLog(scSleepRoomPersonChangeLog);
        }
        return 1;
    }

    /**
     * 删除寝室人员信息
     * 
     * @param id 寝室人员主键
     * @return 结果
     */
    @Override
    public int deleteScSleepRoomPersonById(Long id)
    {
        return scSleepRoomPersonMapper.deleteScSleepRoomPersonById(id);
    }

    /**
     * 批量新增寝室人员
     * @param scSleepRoomPersons 寝室人员数组
     * @return 结果
     */
    @Override
    @Transactional
    public int batchStudent(ScSleepRoomPerson[] scSleepRoomPersons) {
        for (ScSleepRoomPerson scSleepRoomPerson : scSleepRoomPersons) {
            scSleepRoomPerson.setIntime(DateUtils.getLocalDateTime());
            scSleepRoomPersonMapper.insertScSleepRoomPerson(scSleepRoomPerson);

            //增加日志
            ScSleepRoomPersonChangeLog scSleepRoomPersonChangeLog = new ScSleepRoomPersonChangeLog();
            scSleepRoomPersonChangeLog.setParams(scSleepRoomPerson.getId(), scSleepRoomPerson.getSleeproompersontypeid(), null, scSleepRoomPerson.getSleeproomid(), "新增");
            changeLogService.insertLog(scSleepRoomPersonChangeLog);
        }

        return 1;
    }

    /**
     * 获取绑定人员id的数量，有则只有一个， 没有则无
     * @param personId 人员id
     * @param personTypeIdList 寝室人员类型集合
     * @return
     */
    @Override
    public Integer getBindPersonIdSize(Long personId, List<Long> personTypeIdList) {
        return scSleepRoomPersonMapper.getBindPersonIdSize(personId, personTypeIdList);
    }
}
